package com.example.drss;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import com.example.drss.bean.Menu;
import com.example.drss.db.DBTool;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

public class DishesActivity extends Activity {
	
	String name;
	String path;
	
	TextView nameTextView;
	TextView priceTextView;
	TextView sellerTextView;
	TextView sellernumberTextView;
	TextView scoreTextView;
	TextView tasteTextView;
	CheckBox checkBox;
	ImageView imageView;
	
	Button callButton;
	Button backButton;
	
	DBTool dbTool;
	Cursor cursor;
	Menu menu;
	
	OnClickListener listener=new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.button_call:
				Intent intent=new Intent(Intent.ACTION_CALL);
				Uri phoneNumber=Uri.parse("tel:"+menu.getSellernumber());
				intent.setData(phoneNumber);
				startActivity(intent);
				break;
			case R.id.button_dishes_back:
				setResult(RESULT_CANCELED);
				finish();
				break;
			default:
				break;
			}
		}
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_dishes);
		Intent intent=getIntent();
		name=intent.getStringExtra("name");
		path=intent.getStringExtra("databasePath_dishes");
		//Toast.makeText(DishesActivity.this, name, Toast.LENGTH_SHORT).show();
		dbTool=new DBTool(path);
		dbTool.openDatabase();
		init();
	}
	
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		dbTool.closeDatabase();
	}
	
	private void init() {
		// TODO Auto-generated method stub
		nameTextView=(TextView)findViewById(R.id.textView_dishes_name);
		//imageTextView=(TextView)findViewById(R.id.textView_dishes_name);
		priceTextView=(TextView)findViewById(R.id.textView_dishes_price);
		sellerTextView=(TextView)findViewById(R.id.textView_dishes_seller);
		sellernumberTextView=(TextView)findViewById(R.id.textView_dishes_sellernumber);
		scoreTextView=(TextView)findViewById(R.id.textView_dishes_score);
		tasteTextView=(TextView)findViewById(R.id.textView_dishes_taste);
		checkBox=(CheckBox)findViewById(R.id.checkBox_collection);
		imageView=(ImageView)findViewById(R.id.imageView_dishes_image);
		
		callButton=(Button)findViewById(R.id.button_call);
		backButton=(Button)findViewById(R.id.button_dishes_back);
		menu =new Menu();
		
		cursor=dbTool.queryByName(name);
		if (cursor!=null) {
			//读取所有纪录
			while(cursor.moveToNext()){
				menu.setId(cursor.getInt(0));
				menu.setName(cursor.getString(1));
				menu.setImage(cursor.getString(2));
				menu.setCollection(cursor.getInt(3));
				menu.setPrice(cursor.getString(4));
				menu.setSeller(cursor.getString(5));
				menu.setSellernumber(cursor.getString(6));
				menu.setScore(cursor.getString(7));
				menu.setTaste(cursor.getString(8));
			}
			}
		nameTextView.setText("菜名: "+menu.getName());
		priceTextView.setText("价格： "+menu.getPrice());
		sellerTextView.setText("店家： "+menu.getSeller());
		sellernumberTextView.setText("店家电话： "+menu.getSellernumber());
		scoreTextView.setText("平分： "+menu.getScore());
		tasteTextView.setText("味道： "+menu.getTaste());
		if(menu.getCollection()==1){
			checkBox.setChecked(true);
			//checkBox.setText("已收藏");
		}
		checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				switch (buttonView.getId()) {
				case R.id.checkBox_collection:
					if(isChecked==true){
						menu.setCollection(1);
						dbTool.updateMenu(menu);
						//checkBox.setText("已收藏");
						Toast.makeText(DishesActivity.this, "已收藏", Toast.LENGTH_SHORT).show();	
					}
					else {
						menu.setCollection(0);
						dbTool.updateMenu(menu);
						//checkBox.setText("未收藏");
						Toast.makeText(DishesActivity.this, "取消收藏", Toast.LENGTH_SHORT).show();		
					}
					break;
				default:
					break;
				}
			}
		});	
		
		imageView.setImageBitmap(loadBitmap(menu.getImage()+".jpg"));
		
		callButton.setOnClickListener(listener);
		backButton.setOnClickListener(listener);
		
		}
	
	private Bitmap loadBitmap(String filename){
		Resources resources=getResources();
		try {
			InputStream is=resources.getAssets().open(filename);
			Bitmap bitmap=BitmapFactory.decodeStream(is);
			return bitmap;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}
